#include "Game.h"

using namespace std;

Game::Game()
{
	//ctor
	m_isEixt = false;
//	m_Player_lst.clear();
}

Game::~Game()
{
	//dtor
//	m_Player_lst.clear();

	// 释放数据库
	m_GameDb.Dispose();
}

// YU_TODO:
void CallTcpServerLoop(TcpServer* _pSock)
{
	_pSock->ConnectLoop();
	//Game::GetInstance().GetSock()->ConnectLoop();
}

void Game::Init()
{
	/// 配置读取
	Config::LoadGlobalConfig("LoginGlobalConfig.txt");

	/// 启用数据库
	string host = Config::globalConfig_map["LoginDBHost"];
	string user = Config::globalConfig_map["LoginDBUser"];
	string pwd = Config::globalConfig_map["LoginDBPwd"];
	string db = Config::globalConfig_map["LoginDB"];

	m_GameDb.Init(host, user, pwd, db);

	/// 网络启动
	// 服务器部分Test
    string loginIp = Config::globalConfig_map["LoginServerIp"];
    uint16_t loginPort = std::atoi( Config::globalConfig_map["LoginServerPort"].c_str() );
    if(0 == m_ServerSock.Init(loginIp, loginPort)){
		std::thread thread1(CallTcpServerLoop, &m_ServerSock);
		thread1.detach();
    }
}


void Game::Dispose()
{

}


void Game::GameLoop(){
	Debug::State("Start ", __func__);
	int i = 0;

	while(!m_isEixt){
//		// 处理链接列表验证信息验证消息
//		i++;
//		if(i%50 == 1){
//			i = 0;
//
//			// 对每个链接的客户端 进行验证 可以理解为gateway 的功能
//
//			list<Client> Client_lst = m_ServerSock.GetClientList();
//			int socketClientCount = Client_lst.size();
//			int serverInfoCount = m_Login.GetServerCount();
//			int clientInfoCount = m_Login.GetClientCount();
//
//			if(socketClientCount == 0)
//			{
//				continue;
//			}
//
//			if(socketClientCount == (serverInfoCount + clientInfoCount))
//			{
//				continue;
//			}
//
//			list<Client>::iterator iter = Client_lst.begin();
//			for(;iter != Client_lst.end();)
//			{
//				if(iter->m_isPassCheck )
//				{
//					continue;
//				}
//
////				if(iter->m_askTimes>=3)
////				{
////					Game::GetInstance().GetSock()->ShutDownAConnect(iter->m_fd);
////					continue;
////				}
//
//				iter->m_askTimes = iter->m_askTimes + 1;
//				Message Msg1(iter->m_fd, eCmd::SYS_Check_Ask);
//				int key = 9527;
//				Msg1.AddInt(key);
//				Message::Send(&m_ServerSock.GetMsgQunue(), &Msg1);
//				Debug::Log("Send Msg eCmd::SYS_Check_Ask");
//
//				iter++;
//			}
//		}

		// 处理ServerSock 发送消息
		if(!m_ServerSock.GetMsgQunue().IsSendEmpty()){
			Message Msg = m_ServerSock.GetMsgQunue().m_MsgSend_que.front();
			m_ServerSock.Send(&Msg);
			m_ServerSock.GetMsgQunue().m_MsgSend_que.pop();
		}

		// 处理ServerSock 接受消息
		if (!m_ServerSock.GetMsgQunue().IsRecvEmpty()){
			Message Msg = m_ServerSock.GetMsgQunue().m_MsgRecv_que.front();
			m_MsgRounter.ServerRounter(&Msg);
			m_ServerSock.GetMsgQunue().m_MsgRecv_que.pop();
		}
	}
}

TcpServer& Game::GetSock()
{
	return m_ServerSock;
}

// exit
void Game::Exit(){
	m_isEixt = true;
}

void Game::SendMsg2Client(Message* _pMsg){

	_pMsg->Encode();

	// YU_TODO: msg router. to server or to client
	int cmd = (int)_pMsg->m_cmd;
	if(( cmd < MSG_CMD_MIN) && (cmd > MSG_CMD_MAX))
	{
		cout<< "Error. cmd beyond limit."<< endl;
		return;
	}



}
